<?php

set_include_path('../include');
error_reporting(E_ALL);
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);


// загрузка конфигурации приложения
$config = new Zend_Config_Ini('../settings.ini', 'development');
Zend_Session::setOptions(
  array(
    'cookie_domain' => '.'.$config->domain
  )
);
Zend_Session::start();
Zend_Registry::set('config', $config);

// создание объекта для системного журнала
$logger = new Zend_Log(new Zend_Log_Writer_Stream($config->logging->file));
Zend_Registry::set('logger', $logger);

// соединение с БД
$params = array(
    'host' => $config->database->hostname,
    'username' => $config->database->username,
    'password' => $config->database->password,
    'dbname' => $config->database->database
);

$db = Zend_Db::factory($config->database->type, $params);
Zend_Registry::set('db', $db);
$db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");


// настройка аутентификации
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session());

/* $currency = new DatabaseObject_Currency($db);
 if ($auth->getIdentity()->currency != '0')
            $currency->load($auth->getIdentity()->currency);
            else
            $currency->load('1');
            //$this->pre($currency);*/
             Zend_Registry::set('currency','грн');

// обработка запроса пользователя
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory(
        $config->paths->base . '/include/Controllers');

$controller->registerPlugin(new CustomControllerAclManager($auth));
// настройки визуализатора
$vr = new Zend_Controller_Action_Helper_ViewRenderer();
$vr->setView(new Templater());

$vr->setViewSuffix('tpl');

Zend_Controller_Action_HelperBroker::addHelper($vr);

/// Zend_cache 
$frontendOptions = array(
   'lifetime' => 7200, // время жизни кэша - 24 часа
   'automatic_serialization' => true
);
 
$backendOptions = array(
    'cache_dir' => '../tmp/' // директория, в которой размещаются файлы кэша
);
 
// получение объекта Zend_Cache_Core
$cache = Zend_Cache::factory('Core',
                             'File',
                             $frontendOptions,
                             $backendOptions);

Zend_Registry::set('cache', $cache);
//********** Begin route categories *************//


$router = Zend_Controller_Front::getInstance()->getRouter();

//$router->removeDefaultRoutes();

$adminroute = new Zend_Controller_Router_Route_Hostname(':name.'.$config->domain, array('controller' => 'shop'));

//$plainPathRoute = new Zend_Controller_Router_Route_Static('');
 $plainPathRoute = new Zend_Controller_Router_Route(
            ':action/*',
            array(
                'controller' => 'shop',
                'action'     => 'index'
            )
        );
$router->addRoute('shop', $adminroute->chain($plainPathRoute));
//******************************************************************************

$adminroute = new Zend_Controller_Router_Route_Hostname(':name.'.$config->domain, array('controller' => 'shop'));

//$plainPathRoute = new Zend_Controller_Router_Route_Static('');
 $plainPathRoute = new Zend_Controller_Router_Route(
            ':action/:id/*',
            array(
                'controller' => 'shop',
                'action'     => 'index'
            )
        );
$router->addRoute('shopproduct', $adminroute->chain($plainPathRoute));

$route = new Zend_Controller_Router_Route(
        'index/search/material/:material/', array('controller' => 'index',
    'action' => 'search')
);

$controller->getRouter()->addRoute('searchmaterial', $route);

$route = new Zend_Controller_Router_Route(
        'index/search/tags/:tags/', array('controller' => 'index',
    'action' => 'search')
);

$controller->getRouter()->addRoute('searchtags', $route);

$route = new Zend_Controller_Router_Route(
        'admin/zakaz/:id/', array('controller' => 'admin',
    'action' => 'zakaz')
);

$controller->getRouter()->addRoute('zakaz', $route);

$route = new Zend_Controller_Router_Route(
        'account/zakaz/:id/', array('controller' => 'account',
    'action' => 'zakaz')
);

$controller->getRouter()->addRoute('zakazview', $route);

$route = new Zend_Controller_Router_Route(
        'catalogue/', array('controller' => 'index',
    'action' => 'catalog')
);

$controller->getRouter()->addRoute('catalog', $route);

$route = new Zend_Controller_Router_Route_Regex(
        'catalogue/(.+)\.html', array('controller' => 'index',
    'action' => 'catalog'),array(1=>'cat'),'catalogue/%s.html'
);

$controller->getRouter()->addRoute('categorylist', $route);

$route = new Zend_Controller_Router_Route_Regex(
        'catalogue/(.+)/(.+)\.html', array('controller' => 'index',
    'action' => 'catalog'),array(1=>'cat',2=>'subcat'),'catalogue/%s/%s.html'
);

$controller->getRouter()->addRoute('categorylistsub', $route);

$route = new Zend_Controller_Router_Route_Regex(
        'catalogue/(.+)/(.+)/(.+)\.html', array('controller' => 'index',
    'action' => 'catalog'),array(1=>'cat',2=>'subcat',3=>'subsubcat'),'catalogue/%s/%s/%s.html'
);

$controller->getRouter()->addRoute('categorylistsubsub', $route);

$route = new Zend_Controller_Router_Route_Regex(
        'catalogue/(.+)/(.+)/(.+)/(.+)\.html', array('controller' => 'index',
    'action' => 'catalog'),array(1=>'cat',2=>'subcat',3=>'subsubcat',4=>'subsubsubcat'),'catalogue/%s/%s/%s/%s.html'
);

$controller->getRouter()->addRoute('categorylistsubsubsub', $route);


$route = new Zend_Controller_Router_Route(
        'admin/users/edit/:id/', array('controller' => 'admin',
    'action' => 'editusers')
);

$controller->getRouter()->addRoute('admineditusers', $route);

$route = new Zend_Controller_Router_Route(
        'admin/products/edit/:id/', array('controller' => 'admin',
    'action' => 'editproducts')
);

$controller->getRouter()->addRoute('admineditproducts', $route);


$route = new Zend_Controller_Router_Route(
        'account/requests/', array('controller' => 'account',
    'action' => 'requests')
);

$controller->getRouter()->addRoute('requests', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/out/', array('controller' => 'account',
    'action' => 'ownrequests')
);

$controller->getRouter()->addRoute('outrequests', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/declined/', array('controller' => 'account',
    'action' => 'declinedrequests')
);

$controller->getRouter()->addRoute('declinedrequests', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/decline/:id/', array('controller' => 'account',
    'action' => 'declinerequest')
);

$controller->getRouter()->addRoute('declinerequest', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/approve/:id/', array('controller' => 'account',
    'action' => 'approverequest')
);

$controller->getRouter()->addRoute('approverequest', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/approve/all/', array('controller' => 'account',
    'action' => 'approveallrequests')
);

$controller->getRouter()->addRoute('approveallrequests', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/decline/all/', array('controller' => 'account',
    'action' => 'declineallrequests')
);

$controller->getRouter()->addRoute('declineallrequests', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/out/cancel/:id/', array('controller' => 'account',
    'action' => 'cancelrequest')
);

$controller->getRouter()->addRoute('cancelrequest', $route);

$route = new Zend_Controller_Router_Route(
        'account/requests/out/cancel/all/', array('controller' => 'account',
    'action' => 'cancelallrequests')
);

$controller->getRouter()->addRoute('cancelallrequests', $route);

$route = new Zend_Controller_Router_Route(
        'account/messages/viewdialog/:id/', array('controller' => 'account',
    'action' => 'viewdialog')
);

$controller->getRouter()->addRoute('viewdialog', $route);

$route = new Zend_Controller_Router_Route(
        'account/dialogdelete/:id/', array('controller' => 'account',
    'action' => 'dialogdelete')
);

$controller->getRouter()->addRoute('deletedialog', $route);

$route = new Zend_Controller_Router_Route(
        'account/friends/delete/:id/', array('controller' => 'account',
    'action' => 'deletefriend')
);

$controller->getRouter()->addRoute('deletefriend', $route);

$route = new Zend_Controller_Router_Route(
        'account/products/', array('controller' => 'account',
    'action' => 'products')
);

$controller->getRouter()->addRoute('editcategory', $route);

$route = new Zend_Controller_Router_Route(
        'account/products/edit/:id/*', array('controller' => 'account',
    'action' => 'editproducts')
);

$controller->getRouter()->addRoute('editproducts', $route);

$route = new Zend_Controller_Router_Route(
        'product/:id/*', array('controller' => 'products',
    'action' => 'viewproducts')
);

$controller->getRouter()->addRoute('viewproducts', $route);

$route = new Zend_Controller_Router_Route(
        'account/products/delete/:id/*', array('controller' => 'account',
    'action' => 'deleteproducts')
);

$controller->getRouter()->addRoute('deleteproducts', $route);

$route = new Zend_Controller_Router_Route(
        'account/products/add/*', array('controller' => 'account',
    'action' => 'addproducts')
);

$controller->getRouter()->addRoute('addproducts', $route);

$route = new Zend_Controller_Router_Route(
        'account/products/add/complete/*', array('controller' => 'account',
    'action' => 'addproductscomplete')
);

$controller->getRouter()->addRoute('addproductscomplete', $route);

$route = new Zend_Controller_Router_Route(
        'account/products/edit/complete/*', array('controller' => 'account',
    'action' => 'editproductscomplete')
);

$controller->getRouter()->addRoute('editproductscomplete', $route);

//********** End route categories *************//
$route = new Zend_Controller_Router_Route(
        'admin/categories/', array('controller' => 'admin',
    'action' => 'category')
);

$controller->getRouter()->addRoute('editcategory', $route);

$route = new Zend_Controller_Router_Route(
        'admin/categories/edit/:id/*', array('controller' => 'admin',
    'action' => 'editcategory')
);

$controller->getRouter()->addRoute('editcategory', $route);

$route = new Zend_Controller_Router_Route(
        'category/:id/*', array('controller' => 'categories',
    'action' => 'viewcategory')
);

$controller->getRouter()->addRoute('viewcategory', $route);

$route = new Zend_Controller_Router_Route(
        'admin/categories/delete/:id/*', array('controller' => 'admin',
    'action' => 'deletecategory')
);

$controller->getRouter()->addRoute('deletecategory', $route);

$route = new Zend_Controller_Router_Route(
        'admin/categories/add/*', array('controller' => 'admin',
    'action' => 'addcategory')
);

$controller->getRouter()->addRoute('addcategory', $route);

$route = new Zend_Controller_Router_Route(
        'admin/categories/add/complete/*', array('controller' => 'admin',
    'action' => 'addcategorycomplete')
);

$controller->getRouter()->addRoute('addcategorycomplete', $route);

$route = new Zend_Controller_Router_Route(
        'admin/category/edit/complete/*', array('controller' => 'admin',
    'action' => 'editcategorycomplete')
);

$controller->getRouter()->addRoute('editcategorycomplete', $route);

$route = new Zend_Controller_Router_Route(
        'admin/categories/sub/:cat/*', array('controller' => 'admin',
    'action' => 'categories')
);

$controller->getRouter()->addRoute('admincatsub', $route);

$route = new Zend_Controller_Router_Route(
        'basket/index/', array('controller' => 'basket',
    'action' => 'index')
);

$controller->getRouter()->addRoute('cart', $route);

$route = new Zend_Controller_Router_Route(
            'admin/globalsettings/edit/:id/*', array('controller' => 'admin',
        'action' => 'editconfig')
    );

    $controller->getRouter()->addRoute('editconfig', $route);
    
    $route = new Zend_Controller_Router_Route(
            'admin/globalsettings/delete/:id/*', array('controller' => 'admin',
        'action' => 'deleteconfig')
    );

    $controller->getRouter()->addRoute('deleteconfig', $route);
    
    $route = new Zend_Controller_Router_Route(
        'help/category/:name/', array('controller' => 'help',
        'action' => 'viewcat')
    );

    $controller->getRouter()->addRoute('helpcat', $route);
    
    $route = new Zend_Controller_Router_Route(
        'help/item/:alias/', array('controller' => 'help',
        'action' => 'page')
    );

    $controller->getRouter()->addRoute('helppage', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/add/category/', array('controller' => 'admin',
        'action' => 'addhelpcat')
    );

    $controller->getRouter()->addRoute('addhelpcat', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/delete/category/:id', array('controller' => 'admin',
        'action' => 'deletehelpcat')
    );

    $controller->getRouter()->addRoute('deletehelpcat', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/edit/category/:id', array('controller' => 'admin',
        'action' => 'edithelpcat')
    );

    $controller->getRouter()->addRoute('edithelpcat', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/view/category/:id', array('controller' => 'admin',
        'action' => 'viewhelpcat')
    );

    $controller->getRouter()->addRoute('viewhelpcat', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/:cat/page/add/*', array('controller' => 'admin',
        'action' => 'addhelppage')
    );

    $controller->getRouter()->addRoute('addhelppage', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/:cat/page/edit/:id/*', array('controller' => 'admin',
        'action' => 'edithelppage')
    );

    $controller->getRouter()->addRoute('edithelppage', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/help/:cat/page/delete/:id/*', array('controller' => 'admin',
        'action' => 'deletehelppage')
    );

    $controller->getRouter()->addRoute('deletehelppage', $route);
    
    
   
    
    $route = new Zend_Controller_Router_Route(
        'admin/tarif/edit/:id/*', array('controller' => 'admin',
        'action' => 'edittarif')
    );

    $controller->getRouter()->addRoute('edittarif', $route);
    
    $route = new Zend_Controller_Router_Route(
        'admin/tarif/delete/:id/*', array('controller' => 'admin',
        'action' => 'deletetarif')
    );

    $controller->getRouter()->addRoute('deletetarif', $route);
//********** End route products *************//

$controller->dispatch();

?>